package com.useeinfo.oa.modules.wuHanCommon.base.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.wuHanCommon.base.entity.WuHanUnit;
import com.useeinfo.oa.modules.wuHanCommon.base.enums.MeterReadingEnum;
import com.useeinfo.oa.modules.wuHanCommon.base.enums.PointSystem;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("WuHanUnitDao")
public class WuHanUnitDao implements CrudDao<WuHanUnit> {
    private final static Logger logger = LoggerFactory.getLogger(WuHanUnitDao.class);

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {
        Map<String, Object> conditionHash = new HashMap<>();

        conditionHash.put("deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }
        String unit = queryHash.get("unit");
        if (StringUtils.isNotBlank(unit)) {
            conditionHash.put("unit = ?{paramIndex} ", unit);
        }
        String meterReading = queryHash.get("meterReading");
        if (StringUtils.isNotBlank(meterReading)) {
            conditionHash.put("meterReadingEnum = ?{paramIndex} ", MeterReadingEnum.valueOf(meterReading));
        }
        String pointSystem = queryHash.get("pointSystem");
        if (StringUtils.isNotBlank(pointSystem)) {
            conditionHash.put("pointSystem = ?{paramIndex} ", PointSystem.valueOf(pointSystem));
        }
        String magicLike = queryHash.get("magicLike");
        if (StringUtils.isNotBlank(magicLike)) {
            conditionHash.put("unit like ?{paramIndex} ", "%" + magicLike + "%");
        }
        String magicId = queryHash.get("magicId");
        if (StringUtils.isNotBlank(magicId)) {
            conditionHash.put("id != ?{paramIndex} ", StringConverters.ToLong(magicId));
        }

        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(w) from WuHanUnit w ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<WuHanUnit> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select w from WuHanUnit w ", "order by w.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<WuHanUnit> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, WuHanUnit.class);
        return queryParam.findPageList(typedQuery);
    }

    @Override
    public WuHanUnit findModel(Long id) {
        return entityManager.find(WuHanUnit.class, id);
    }

    @Override
    public Integer add(WuHanUnit model) {
        model.setCreateDate(new Date());
        entityManager.persist(model);
        logger.info("WuHanUnitDaoImpl添加WuHanUnit成功！");
        return 1;
    }

    @Override
    public Integer update(WuHanUnit model) {
        WuHanUnit wuHanUnit = entityManager.find(WuHanUnit.class, model.getId());
        wuHanUnit.setUpdateDate(new Date());
        wuHanUnit.setMeterReadingEnum(model.getMeterReadingEnum());
        wuHanUnit.setUnit(model.getUnit());
        wuHanUnit.setPointSystem(model.getPointSystem());
        return 1;
    }

    @Override
    public Integer delete(Long id) {
        WuHanUnit wuHanUnit = entityManager.find(WuHanUnit.class, id);
        entityManager.remove(wuHanUnit);
        return 1;
    }

    @Override
    public Integer deleteLogic(WuHanUnit model) {
        model.setDeleteStatus(true);
        return 1;
    }
}
